//
//  GroupSelectedView.m
//  ChatForHot
//
//  Created by wr on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "GroupSelectedView.h"

@interface GroupSelectedView ()

@property (nonatomic, strong) UIImageView *HOTCHATmoveview;
@property (nonatomic, strong) UIButton *HOTCHATleftbutton;
@property (nonatomic, strong) UIButton *HOTCHATrightButton;
@property (nonatomic, assign) BOOL HOTCHATisRight;
@end

@implementation GroupSelectedView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (void)HOTCHATdidInitialized {
    
    self.HOTCHATleftbutton = [[UIButton alloc] init];
    [self.HOTCHATleftbutton setTitle:NSLocalizedString(@"", nil) forState:(UIControlStateNormal)];
    self.HOTCHATleftbutton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.HOTCHATleftbutton setTitleColor:[UIColor colorFromHexString:@"#111111"] forState:(UIControlStateNormal)];
    [self.HOTCHATleftbutton setTitleColor:[UIColor colorFromHexString:@"#8123FF"] forState:(UIControlStateSelected)];
    [self.HOTCHATleftbutton addTarget:self action:@selector(leftButSelected) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.HOTCHATleftbutton];
    
    self.HOTCHATrightButton = [[UIButton alloc] init];
    [self.HOTCHATrightButton setTitle:NSLocalizedString(@"", nil) forState:(UIControlStateNormal)];
    self.HOTCHATrightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.HOTCHATrightButton setTitleColor:[UIColor colorFromHexString:@"111111"] forState:(UIControlStateNormal)];
    [self.HOTCHATrightButton setTitleColor:[UIColor colorFromHexString:@"8123FF"] forState:(UIControlStateSelected)];
    [self.HOTCHATrightButton addTarget:self action:@selector(rightButSelected) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.HOTCHATrightButton];
    
    self.HOTCHATmoveview = [[UIImageView alloc] init];
    self.HOTCHATmoveview.image = [UIImage imageNamed:@"p_album_ic_triangle"];
    [self addSubview:self.HOTCHATmoveview];
    
    [self.HOTCHATleftbutton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(32);
        make.top.mas_equalTo(0);
        make.right.equalTo(self.mas_centerX).offset(-10);
    }];
    
    [self.HOTCHATrightButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(32);
        make.top.mas_equalTo(0);
        make.left.equalTo(self.mas_centerX).offset(10);
    }];
    
    [self.HOTCHATmoveview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(7);
        make.width.mas_equalTo(17);
        make.centerX.equalTo(self.HOTCHATleftbutton);
        make.top.equalTo(self.HOTCHATleftbutton.mas_bottom).mas_offset(1);
    }];
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(152, 40);
}

- (void)updateConstraints{
    [super updateConstraints];
    
    [self.HOTCHATmoveview mas_remakeConstraints:^(MASConstraintMaker *make) {
        if (self.HOTCHATisRight) {
            make.centerX.equalTo(self.HOTCHATrightButton);
            make.top.equalTo(self.HOTCHATrightButton.mas_bottom).mas_offset(1);
        }else{
            make.centerX.equalTo(self.HOTCHATleftbutton);
            make.top.equalTo(self.HOTCHATleftbutton.mas_bottom).mas_offset(1);
        }
    }];
}

- (void)leftButSelected {
    if (!self.HOTCHATisRight) {
        return;
    }
    self.HOTCHATisRight = NO;
    self.HOTCHATleftbutton.selected = YES;
    self.HOTCHATrightButton.selected = NO;
    [self HOTCHATAnimationmove];
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATSelectedViewWithIndex:)]) {
        [self.delegate HOTCHATSelectedViewWithIndex:1];
    }
}

- (void)rightButSelected {
    if (self.HOTCHATisRight) {
        return;
    }
    self.HOTCHATisRight = YES;
    self.HOTCHATleftbutton.selected = NO;
    self.HOTCHATrightButton.selected = YES;
    [self HOTCHATAnimationmove];
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATSelectedViewWithIndex:)]) {
        [self.delegate HOTCHATSelectedViewWithIndex:2];
    }
}

- (void)HOTCHATAnimationmove{
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)setHOTCHATleftTitle:(NSString *)HOTCHATleftTitle {
    _HOTCHATleftTitle = HOTCHATleftTitle;
    [self.HOTCHATleftbutton setTitle:HOTCHATleftTitle forState:(UIControlStateNormal)];
}

- (void)setHOTCHATrightTitle:(NSString *)HOTCHATrightTitle {
    _HOTCHATrightTitle = HOTCHATrightTitle;
    [self.HOTCHATrightButton setTitle:HOTCHATrightTitle forState:(UIControlStateNormal)];
}

- (void)setHOTCHATmidSpace:(float)HOTCHATmidSpace {
    _HOTCHATmidSpace = HOTCHATmidSpace;
    
    [self.HOTCHATleftbutton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(-HOTCHATmidSpace);
    }];
    [self.HOTCHATrightButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(HOTCHATmidSpace);
    }];
}
@end
